<?php
namespace Home\Controller;
use Think\Controller;
class RegController extends Controller {
    public function Index(){
    	$question = D('Vercode')->getOneVerify();
    	$this->assign('question',$question);
        $this->assign('title','注册');
        $this->display();
    }

    public function checkData()
    {
    	$password = I('post.pass');
    	$repassword = I('post.repass');

    	if ($password!=$repassword) {
    		$arr = array(
    			'error'=>1,
    			'info'=>"两次密码不一样"
    			);
    		echo json_encode($arr);
    		exit;
    	}

    	$vercode = I('post.vercode');

    	if ($vercode != $_SESSION['answer']) {
    		$arr = array(
    			'error'=>1,
    			'info'=>"人类验证失败"
    			);
    		echo json_encode($arr);
    		exit;
    	}

    	if (D('user')->checkEmail(I('post.email'))) {
    		$arr = array(
    			'error'=>1,
    			'info'=>"该邮箱已被注册"
    			);
    		echo json_encode($arr);
    		exit;
    	}

    	if (D('user')->checkName(I('post.username'))) {
    		$arr = array(
    			'error'=>1,
    			'info'=>"该昵称已被注册"
    			);
    		echo json_encode($arr);
    		exit;
    	}

        $headface='Public/image/head.jpg';
    	$data = I('post.');
    	$data['pass'] = md5($password);
    	$data['regtime'] = time();
        $data['headface'] = $headface;
    	
    	if (M('user')->add($data)) {
            // var_dump($data);
            $_SESSION['username']=$data['username'];
            $_SESSION['email']=$data['email'];
            $_SESSION['headface']=$data['headface'];
    		$arr = array(
    			'error'=>0,
    			'info'=>"注册成功"
    			);
    		echo json_encode($arr);
    	}else{
    		$arr = array(
    			'error'=>1,
    			'info'=>"注册失败"
    			);
    		echo json_encode($arr);
    	}
    }




}